package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.FollowMasterListInfo;
import com.shxc.smarttrade.old.myinterface.FollowMasterClickListener;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class FollowMasterNowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<FollowMasterListInfo.DataBean.MasterBean> mastersBeen;
    private LayoutInflater mInflater;
    private FollowMasterClickListener listener;

    public FollowMasterNowAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<FollowMasterListInfo.DataBean.MasterBean> mastersBeen) {
        this.mastersBeen = mastersBeen;
        notifyDataSetChanged();
    }

    public void setMyItemCLickListener(FollowMasterClickListener listener) {
        this.listener = listener;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MasterHolder(mInflater.inflate(R.layout.adapter_follow_order_now_list, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MasterHolder) {
            final FollowMasterListInfo.DataBean.MasterBean mb = mastersBeen.get(position);
            Glide.with(mContext).load(mb.getHeaderLink()).into(((MasterHolder) holder).ivHead);
            ((MasterHolder) holder).tvName.setText(mb.getUserName().trim());
            if (mb.getCopyProfit() < 0) {
                ((MasterHolder) holder).tvDivide.setTextColor(mContext.getResources().getColor(R.color.stock_fell_color));
                ((MasterHolder) holder).tvDivide.setText("-$" + BaseUtils.getDigitsData(Math.abs(mb.getCopyProfit()), 2));
            } else {
                ((MasterHolder) holder).tvDivide.setTextColor(mContext.getResources().getColor(R.color.stock_rise_color));
                ((MasterHolder) holder).tvDivide.setText("$" + BaseUtils.getDigitsData(mb.getCopyProfit(), 2));
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemCLick(position, mb);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mastersBeen == null ? 0 : mastersBeen.size();
    }

    public class MasterHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_head)
        CircleImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_divide)
        TextView tvDivide;

        MasterHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }
}
